package com.aiti.lulian.service.impl;

import cn.hutool.core.date.DateUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.entity.crad.CardZfSort;
import com.aiti.lulian.entity.crad.cluster.CardSortOld;
import com.aiti.lulian.entity.crad.cluster.CardZfFloorText;
import com.aiti.lulian.mapper.card.cluster.CardZfFloorTextMapper;
import com.aiti.lulian.mapper.card.cluster.CardZfSortMapper;
import com.aiti.lulian.service.ICardZfFloorTextService;
import com.aiti.lulian.service.ICardZfSortService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CardZfSortServiceImpl extends BaseSerivceImpl<CardZfSortMapper, CardZfSort> implements ICardZfSortService {

    private final CardZfSortMapper baseMapper;

    public CardZfSortServiceImpl(CardZfSortMapper baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public Message selectList(String userId) {

        List<CardZfSort> list = baseMapper.selectList(new QueryWrapper<CardZfSort>().eq("enterprise_id", userId));
//        if(list == null || list.isEmpty()){
//            List<CardSortOld> cardSortOlds = baseMapper.oldList(userId);
//            return Message.success(cardSortOlds);
//        }
        return Message.success(list);
    }

    @Override
    public Message selectWeChatList(String enterpriseId) {
        QueryWrapper<CardZfSort> qw = new QueryWrapper<CardZfSort>().eq("enterprise_id", enterpriseId);
        qw.orderByAsc("sort");
        qw.eq("is_show","0");
        List<CardZfSort> list = baseMapper.selectList(qw);
        return Message.success(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveData(List<CardZfSort> zfSort, String userId) {
        //删除原有的数据
        baseMapper.delete(new QueryWrapper<CardZfSort>().eq("baseMapper", userId));
        List<CardZfSort> list = new ArrayList<>();
        for(CardZfSort temp : zfSort){
            temp.setBaseCreateBy(userId);
            temp.setBaseCreateTime(DateUtil.date());
            list.add(temp);
        }
        if(!list.isEmpty()){
            saveBatch(list);
        }
    }


}
